// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      user: {},
      isName: false,
      isGender: false,
      isBrith: false,
      value: '',
      cursor: '',
      columns: ['男', '女'],
      gender: '',
      birthday: '',
  },
  onName() {
      this.setData({
          isName: true,
          value: this.data.user.name,
          cursor: this.data.user.name.length
      })
  },
  onGender() {
      this.setData({
          isGender: true,
          gender: this.data.user.gender
      })
  },
  onBrith() {
      this.setData({
          isBrith: true
      })
  },
  onClickLeft() {
      this.setData({
          isName: false
      })
  },
  onClickRight() {
      this.editUser({
          name: this.data.value
      })
      this.onClickLeft()
  },
  onGenderLeft() {
      this.setData({
          isGender: false
      })
  },
  onGenderRight() {
      this.editUser({
          gender: this.data.gender
      })
      this.onGenderLeft()
  },
  onBrithLeft() {
      this.setData({
          isBrith: false
      })
  },
  onBrithRight() {
      let time = new Date(this.data.birthday).toLocaleString().slice(0, -10).split('/')
      const res = time.map(i => i < 10 ? `0${i}` : i)
      this.editUser({
          birthday: res.join('-')
      })
      this.onBrithLeft()
  },
  editName(e) {
      this.setData({
          value: e.detail.value,
          cursor: e.detail.cursor
      })
  },
  onChange(event) {
      const {
          picker,
          value,
          index
      } = event.detail;
      this.setData({
          gender: index
      })
  },
  onInput(event) {
      this.setData({
          birthday: event.detail,
      })
  },
  //点击上传头像方法
  chooseWxImage() {
      wx.chooseMedia({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: ['album'],
          success: (res) => {
              console.log(res);
              var tempFilePaths = res.tempFiles[0].tempFilePath;
              wx.navigateTo({
                  url: "/pages/avatarCut/avatarCut?src=" + tempFilePaths
              });
          }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.userInfo()
  },
  userInfo() {
      wx.request({
          url: 'http://toutiao.itheima.net/v1_0/user/profile',
          method: 'GET',
          header: {
              Authorization: `Bearer ${wx.getStorageSync("token")}`
          },
          success: ({
              data: {
                  data
              }
          }) => {
              this.setData({
                  user: data,
                  value: data.name,
                  cursor: data.name.length,
                  gender: data.gender,
                  birthday: new Date(data.birthday.split('-').join(',')).getTime(),
              })
          }
      })
  },
  editUser(data) {
      wx.request({
          url: 'http://toutiao.itheima.net/v1_0/user/profile',
          method: 'PATCH',
          header: {
              Authorization: `Bearer ${wx.getStorageSync("token")}`
          },
          data,
          success: () => {
              this.userInfo()
          }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})